package com.example.stringbufferlogger.Utils;

import bea.jolt.JoltRemoteService;
import bea.jolt.JoltSession;
import bea.jolt.JoltSessionAttributes;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JoltUtil {
    public static final String OLD_SYS = "OLD_SYS";//调用老系统标识
    public static final String NEW_SYS = "NEW_SYS";//调用新系统标识

    public static final String OLD_SYS_CONFIG_TYPE = "OLD_JOLT_CONFIG"; //配置表CP_CONFIG_ENUMERATION的type
    public static final String NEW_SYS_CONFIG_TYPE = "NEW_JOLT_CONFIG";//配置表CP_CONFIG_ENUMERATION的type

    private static final Integer REQ_MIN_SIZE = 8000;//请求报文最小长度

    private static final String JOLT_ENCODE = "GB18030";//jolt编码格式

    private static String oldServerNo;//老系统调用服务名(例如FTPIS000)
    private static String oldAddress;//老系统地址(//ip:port)
    private static String oldJoltString;
    private static int oldTimeout;

    private static String newServerNo;//新系统调用服务名(例如FTPIS000)
    private static String newAddress;//新系统地址(//ip:port)
    private static String newJoltString;
    private static int newTimeout;

    /**
     * 加载调用老系统jolt配置
     * @param config CP_CONFIG_ENUMERATION
     */
    public static void loadOldConfig(String config) {
        String[] split = config.split("\\|");
        oldServerNo = split[0];
        oldAddress = split[1];
        oldJoltString = split[2];
        oldTimeout = Integer.parseInt(split[3]);
    }

    /**
     * 加载调用新系统jolt配置
     * @param config CP_CONFIG_ENUMERATION
     */
    public static void loadNewConfig(String config) {
        String[] split = config.split("\\|");
        newServerNo = split[0];
        newAddress = split[1];
        newJoltString = split[2];
        newTimeout = Integer.parseInt(split[3]);
    }

    public static String call(String target, String content, Long mark) {
        if (OLD_SYS.equals(target)) {
            return joltCall(oldServerNo, oldAddress, content, oldJoltString, oldTimeout, mark);
        } else if(NEW_SYS.equals(target)) {
            return joltCall(newServerNo, newAddress, content, newJoltString, newTimeout, mark);
        } else {
            throw new RuntimeException("调用jolt类型错误");
        }
    }

    private static String joltCall(String serverNo, String address, String content, String joltString, int idleTimeout, Long mark) {
        System.setProperty("bea.jolt.encoding", JOLT_ENCODE);
        JoltSession session;
        JoltSessionAttributes attr;
        JoltRemoteService service;
        String userName = null;
        String userPassword = null;
        String appPassword = null;
        String userRole = null;
        attr = new JoltSessionAttributes();
        attr.setString(JoltSessionAttributes.APPADDRESS, address);
        attr.setInt(JoltSessionAttributes.IDLETIMEOUT, idleTimeout);

        session = new JoltSession(attr, userName, userRole, userPassword, appPassword);
        service = new JoltRemoteService(serverNo, session);

        try {
            String contentFormat = String.format("%-" + REQ_MIN_SIZE + "s", content);//字符串左对齐，长度不满8000在右侧用空格填充
            log.info("Jolt请求-address:[{}],ServerNo:[{}],日志标识:[{}],请求内容:[{}]", address, serverNo, mark, contentFormat);
            service.setString(joltString, contentFormat);

            long startTime = System.currentTimeMillis();
            service.call(null);
            String result = service.getStringDef(joltString, null);
            long endTime = System.currentTimeMillis();
            log.info("Jolt响应-address:[{}],ServerNo:[{}],日志标识:[{}],响应内容:[{}],耗时:[{}ms]", address, serverNo, mark, result, endTime - startTime);

            return result;
        } catch (Exception e) {
            log.error("日志标识:[{}],调用jolt异常", mark, e);
            throw new RuntimeException("调用Jolt异常");
        } finally {
            service.clear();
            session.endSession();
        }
    }

}

